﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using Newtonsoft.Json;
using ZX.BPM.Library;
using ZX.EHR.Library;
using ZX.BLL;
using ZX.Common;
using ZX.BLL.Extend;
using ZX.DBModule;
namespace ZX.WebApp.Mobi.proc
{
    /// <summary>
    /// appgetdata 的摘要说明
    /// </summary>
    public class appgetdata : IHttpHandler
    {

        DatUser user = new DatUser();
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string type = context.Request["callback"];
            string para = context.Request["para"];
            context.Response.Write(Proc(type, para));
        }
        private string Proc(string type, string para)
        {
            
            string back = "";
            switch (type)
            {
                case "GetTaskCount":
                    back = GetTaskCount();
                    break;
                case "UnLoad":
                    back = UnLoad();
                    break;
                case "GetTask":
                    back = GetTask(para);
                    break;
                case "LoadDeptTree":
                    back = (new ZX.Web.UI.Controls.TreeView()).createTreeHtml("@001-001");
                    break;
                case "GetUser":
                    back = GetUser(para);
                    break;
                case "LoginBack":
                    back = CheckLogin(para);
                    break;
            }
            return type+"("+back+");";
        }
        public string CheckLogin(string unamepwd)
        {
            string[] up = unamepwd.Split(';');
            BUser buser = new BUser();
            DatUser dtusr = new DatUser();
            dtusr.LoginName = up[0].Trim();
            dtusr.Password = ExtendMethods.GetMd5Hash(up[1].Trim());
            DatUser user = buser.GetDatUserByLoginNameAndPwd(dtusr);

            if (user == null)
            {
                return "{\"back\":\"N\"}";
            }
            else
            {
                //用户登录时判断是否过期
                if (buser.IsExpire(dtusr))
                {
                    return "{\"back\":\"P\"}";
                }
                else
                {
                    WriteLog(user);
                    return "{\"back\":\""+user.UserID+"\"}";
                }
            }
        }

        /// <summary>
        /// 写登录日志
        /// </summary>
        private void WriteLog(DatUser user)
        {
            BaseBussnies buss = new BaseBussnies();
            SysDataLog log = new SysDataLog();
            log.UserID = user.UserID;
            log.Action = "NativeAPP_Login";
            log.Data = "IP:" + HttpContext.Current.Request.UserHostAddress + ",LoginName:" + user.LoginName;
            log.EntityName = "DatUser";
            log.LogTime = DateTime.Now;
            buss.entity = log;
            buss.Insert();
        }
        //获取待办总数
        private string GetTaskCount()
        {
            return (new BHF_TaskInstanceView()).GetTaskInstanceCount("", "", user.LoginName, 0, 20).ToString();
        }

        //获取待办
        private string GetTask(string para)
        {
            string[] p = para.Split('︾');
            string selectname = "";
            int firs = Convert.ToInt32(p[0]);
            int size = Convert.ToInt32(p[1]);
            if (p.Length > 2)
            {
                selectname = p[2].ToString();
            }
            return JsonConvert.SerializeObject((new BHF_TaskInstanceView()).GetTaskDatatable("", selectname, user.LoginName, firs, size));
        }
        /// <summary>
        /// 根据部门Or姓名 获取人员
        /// </summary>
        /// <param name="deptid"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        private string GetUser(string value)
        {
            string[] p = value.Split(',');
            string deptid = p[0].ToString();
            string name = null;
            if (p.Length > 1)
            {
                name = p[1].ToString();
            }
            return JsonConvert.SerializeObject((new BHF_TaskInstanceView()).GetDatUser(deptid, name));
        }


        private string UnLoad()
        {
            try
            {
                FormsAuthentication.SignOut();
                HttpContext.Current.Session["User"] = null;
                HttpContext.Current.Session["IsOutSite"] = null;
                HttpContext.Current.Session["vcode"] = null;
                return "1";
            }
            catch (Exception)
            {
                return "0";
            }
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}